# coding=utf-8
# 编译日期：2023-02-06 10:28:13
# 版权所有：www.i-search.com.cn
import ubpa.init_input as iinput
from ubpa.base_util import StdOutHook, ExceptionHandler
import ubpa.ics as ics
import ubpa.icsv as icsv
import ubpa.iexcel as iexcel
import ubpa.itools.rpa_environment as rpa_environment
import ubpa.iie as iie
import ubpa.ikeyboard as ikeyboard
import ubpa.iwin as iwin
import ubpa.ibrowse as ibrowse
import time
import pdb
from ubpa.ilog import ILog
import getopt
from sys import argv
import sys
import os
from ubpa.base_img import *

class GetExchangeRate1:
     
    def __init__(self,**kwargs):
        self.__logger = ILog(__file__)
        self.path = set_img_res_path(__file__)
        self.robot_no = ''
        self.proc_no = ''
        self.job_no = ''
        self.input_arg = ''
        if('robot_no' in kwargs.keys()):
            self.robot_no = kwargs['robot_no']
        if('proc_no' in kwargs.keys()):
            self.proc_no = kwargs['proc_no']
        if('job_no' in kwargs.keys()):
            self.job_no = kwargs['job_no']
        ILog.JOB_NO, ILog.OLD_STDOUT = self.job_no, sys.stdout
        sys.stdout = StdOutHook(self.job_no, sys.stdout)
        ExceptionHandler.JOB_NO, ExceptionHandler.OLD_STDERR = self.job_no, sys.stderr
        sys.excepthook = ExceptionHandler.handle_exception
        if('input_arg' in kwargs.keys()):
            self.input_arg = kwargs['input_arg']
            if(len(self.input_arg) <= 0):
                self.input_arg = iinput.load_init(__file__)
            if self.input_arg is None:
                sys.exit(0)
        self.gv_1=None
      
    def Main(self):
        lv_1=None
        lv_2=None
        #打开浏览器
        self.__logger.dlogs(job_no=self.job_no,logmsg='Flow:Main,StepNodeTag:2022120811431848445,Title:打开浏览器,Note:')
        tvar_2022120811431848446=ibrowse.open_browser(browser_type='ie',url='https://www.boc.cn')
        print('[Main] [打开浏览器] [SNTag:2022120811431848445]  返回值：[' + str(type(tvar_2022120811431848446)) + ']' + str(tvar_2022120811431848446))
        #最大化窗口
        self.__logger.dlogs(job_no=self.job_no,logmsg='Flow:Main,StepNodeTag:2022121210035353513,Title:最大化窗口,Note:')
        tvar_2022121210035353614=iwin.do_win_maximize(waitfor=10.000,win_title=r'中国银行网站_全球门户首页 - Internet Explorer')
        print('[Main] [最大化窗口] [SNTag:2022121210035353513]  返回值：[' + str(type(tvar_2022121210035353614)) + ']' + str(tvar_2022121210035353614))
        #鼠标点击
        self.__logger.dlogs(job_no=self.job_no,logmsg='Flow:Main,StepNodeTag:2022120811431848458,Title:鼠标点击,Note:')
        iie.do_click_pos_v2(waitfor=10.000,run_mode='unctrl',button='left',curson='center',continue_on_error='break',search_mode=0,win_title=r'中国银行网站_全球门户首页 - Internet Explorer',selector=r'#sword',url=r'https://www.boc.cn/')
        #模拟按键
        self.__logger.dlogs(job_no=self.job_no,logmsg='Flow:Main,StepNodeTag:2022120811431848453,Title:模拟按键,Note:')
        ikeyboard.key_send_cs(waitfor=10.000,text='外汇牌价{Enter}')
        #鼠标点击
        self.__logger.dlogs(job_no=self.job_no,logmsg='Flow:Main,StepNodeTag:2022120811431848448,Title:鼠标点击,Note:')
        iie.do_click_pos_v2(waitfor=10.000,run_mode='unctrl',button='left',curson='center',continue_on_error='break',search_mode=0,win_title=r'中国银行全球门户网站 - Internet Explorer',selector=r'FONT[color=#ff0000]',url=r'https://srh.bankofchina.com/search/sitesearch/index.jsp')
        #拾取表格
        self.__logger.dlogs(job_no=self.job_no,logmsg='Flow:Main,StepNodeTag:2022121210084715218,Title:拾取表格,Note:')
        lv_1=iie.get_ie_table_v2(waitfor=10.000,selector=r'DIV:nth-of-type(1) > DIV:nth-of-type(5) > DIV:nth-of-type(1) > DIV:nth-of-type(2) > TABLE:nth-of-type(1)',url=r'https://www.bankofchina.com/sourcedb/whpj/?keywords=外汇牌价',continue_on_error='break',search_mode=0)
        print('[Main] [拾取表格] [SNTag:2022121210084715218]  返回值：[' + str(type(lv_1)) + ']' + str(lv_1))
        #获取环境路径
        self.__logger.dlogs(job_no=self.job_no,logmsg='Flow:Main,StepNodeTag:2022120811431848449,Title:获取环境路径,Note:')
        tvar_2022120811431848450=rpa_environment.get_sys_path(name='Desktop')
        print('[Main] [获取环境路径] [SNTag:2022120811431848449]  返回值：[' + str(type(tvar_2022120811431848450)) + ']' + str(tvar_2022120811431848450))
        #创建excel
        self.__logger.dlogs(job_no=self.job_no,logmsg='Flow:Main,StepNodeTag:2022120811431848454,Title:创建excel,Note:')
        lv_2=iexcel.create_excel(path=tvar_2022120811431848450,file_name='获取汇率')
        print('[Main] [创建excel] [SNTag:2022120811431848454]  返回值：[' + str(type(lv_2)) + ']' + str(lv_2))
        #导出excel
        self.__logger.dlogs(job_no=self.job_no,logmsg='Flow:Main,StepNodeTag:2022120811431848451,Title:导出excel,Note:')
        tvar_2022120811431848452=icsv.write_excel(path=lv_2,df=lv_1)
        print('[Main] [导出excel] [SNTag:2022120811431848451]  返回值：[' + str(type(tvar_2022120811431848452)) + ']' + str(tvar_2022120811431848452))
        #文件
        self.__logger.dlogs(job_no=self.job_no,logmsg='Flow:Main,StepNodeTag:2022121415021112452,Title:文件,Note:')
        ics.run_shellexecute(path=lv_2)
 
if __name__ == '__main__':
    ILog.begin_init()
    robot_no = ''
    proc_no = ''
    job_no = ''
    input_arg = ''
    try:
        argv = sys.argv[1:]
        opts, args = getopt.getopt(argv,"hr:p:j:i:",["robot = ","proc = ","job = ","input = "])
    except getopt.GetoptError:
        print ('robot.py -r <robot> -p <proc> -j <job>')
    for opt, arg in opts:
        if opt == '-h':
            print ('robot.py -r <robot> -p <proc> -j <job>')
        elif opt in ("-r", "--robot"):
            robot_no = arg
        elif opt in ("-p", "--proc"):
            proc_no = arg
        elif opt in ("-j", "--job"):
            job_no = arg
        elif opt in ("-i", "--input"):
            input_arg = arg
    pro = GetExchangeRate1(robot_no=robot_no,proc_no=proc_no,job_no=job_no,input_arg=input_arg)
    pro.Main()
